const articleModel = require('../models/article');

class ArticleController {

  /**
   * 创建文章
   */

  static async create(ctx) {
    let {
      title,
      author,
      introduction,
      cover,
      content
    } = ctx.request.body;

    let params = {
      title,
      author,
      introduction,
      cover,
      content
    }

    try {
      let data = await articleModel.create(params);
      ctx.respons.status = 200;
      ctx.body = {
        code: 200,
        message: '创建文章成功',
        data
      }
    } catch (err) {
      ctx.response.status = 500;
      ctx.body = {
        code: 500,
        message: '创建文章失败',
        data: err
      }
    }
  }

  /**
   * 更新文章
   */

  static async update(ctx) {
    let id = ctx.params.id;
    if (!id) {
      ctx.respons.status = 412;
      ctx.body = {
        code: 412,
        message: 'id不能为空'
      }

      return false
    }

    if (isNaN(id)) {
      ctx.response.status = 412;
      ctx.body = {
        code: 412,
        message: '请传入正确的Id'
      }
    }

    let {
      title,
      author,
      introduction,
      cover,
      content
    } = ctx.request.body;

    let params = {
      title,
      author,
      introduction,
      cover,
      content
    }

    try {
        let data = await articleModel.update(id, params);
        ctx.response.status = 200;
        ctx.body = {
            code: 500,
            message: '更新文章成功',
            data: data
        }
    } catch (err) {
        ctx.response.status = 500;
        ctx.body = {
            code: 500,
            message: '更新文章失败',
            data: err
        }
    }
  }


  /**
   * 获取文章列表
   */

  static async list(ctx) {
    let params = ctx.query;
    console.log(params);
    try {
      let data = await articleModel.list(params);
      ctx.response.status = 200;
      ctx.body = {
        code: 200,
        message: '查询文章列表成功',
        data
      }
    } catch (err) {
      ctx.response.status = 500;
      ctx.body = {
        code: 500,
        message: '查询文章列表失败',
        data: err
      }
    }
  }

  /**
   * 
   */
  static async detail(ctx) {
    let { id } = ctx.params;
    if (!id) {
      ctx.response.status = 412;
      ctx.body = {
        code: 412,
        message: '文章id为空,请传入查询的文章id'
      }
      return false;
    }

    if (isNaN(id)) {
      ctx.response.status = 412;
      ctx.body = {
        code: 412,
        message: '请传入正确的文章id'
      }
      return false
    }

    try {
      let data = await articleModel.detail(id);

      ctx.response.status = 200;
      ctx.body = {
        code: 200,
        message: '查询文章成功',
        data
      }
    } catch (err) {
      ctx.response.status = 500;
      ctx.body = {
        code: 500,
        message: '查询文章失败',
        data: err
      }
    }
  }
}
module.exports = ArticleController